﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using iShoppingBUS;
using iShoppingDTO;

namespace iShopping.Guest.UserControl
{
    public partial class TimNangCao : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnTimKiem_Click(object sender, EventArgs e)
        {
           //if (!IsPostBack)
            //{
                phanTrang();
                //ViewState["Time"] = dtime;

            //}
            //else
            //{
                //dtime = (double)ViewState["Time"];
           // }
        }

        public void phanTrang()
        {
            string connString = ConfigurationManager.ConnectionStrings["DauGia"].ConnectionString;
            try
            {
                SqlConnection conn = new SqlConnection(connString);
                conn.Open();
                string sql = "SELECT * FROM SanPham ,LoaiSanPham WHERE SanPham.MaLoaiSanPham = LoaiSanPham.MaLoaiSanPham";
                if (txtLoaiSP.Text != "")
                {
                    sql = sql + " and TenLoaiSanPham = N'"+txtLoaiSP.Text+"'";
                }
                if (txtGiaTu.Text != "")
                {
                    sql = sql + " and GiaKhoiDiem > "+txtGiaTu.Text;
                }
                if (txtGiaDen.Text != "")
                {
                    sql = sql + " and GiaKhoiDiem < " + txtGiaDen.Text;
                }
                if (txtThoiGianKetThuc.Text != "")
                {
                    sql = sql + " and NgayHetHan = '"+txtThoiGianKetThuc.Text+"'";
                }
                SqlDataAdapter dtAdapter = new SqlDataAdapter(sql, conn);
                DataTable resTable = new DataTable();
                if (IsPostBack)
                {
                    dtAdapter.Fill(resTable);
                    lblRecordCount.Text = Convert.ToString(resTable.Rows.Count);
                    resTable = null;
                    resTable = new DataTable();
                }
                dtAdapter.Fill(Convert.ToInt32(lblCurrentIndex.Text), Convert.ToInt32(lblPageSize.Text), resTable);
                DataList1.DataSource = resTable;
                DataList1.DataBind();
                conn.Close();
                conn.Dispose();
            }
            catch (Exception ex)
            {
            }

            hienThiDataList();
        }
        public void hienThiDataList()
        {
            lblCount.Text = "Số mẩu tin: <b>" + lblRecordCount.Text;
            lblCount.Text += "</b> | Trang: <b> ";
            lblCount.Text += Convert.ToString(Convert.ToInt32(lblCurrentIndex.Text) / Convert.ToInt32(lblPageSize.Text) + 1);
            lblCount.Text += " </b> trên tổng số <b>";
            if ((Convert.ToInt32(lblRecordCount.Text) % Convert.ToInt32(lblPageSize.Text)) > 0)
            {
                lblCount.Text += Convert.ToString(Convert.ToInt32(lblRecordCount.Text) / Convert.ToInt32(lblPageSize.Text) + 1);
            }
            else
            {
                lblCount.Text += Convert.ToString(Convert.ToInt32(lblRecordCount.Text) / Convert.ToInt32(lblPageSize.Text));
            }
            lblCount.Text += " </b> trang";
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            lblCurrentIndex.Text = Convert.ToString(Convert.ToInt32(lblCurrentIndex.Text) - Convert.ToInt32(lblPageSize.Text));
            if (Convert.ToInt32(lblCurrentIndex.Text) < 0)
            {
                lblCurrentIndex.Text = "0";
            }
            phanTrang();
        }

        protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
        {
            if ((Convert.ToInt32(lblCurrentIndex.Text) + Convert.ToInt32(lblPageSize.Text)) < Convert.ToInt32(lblRecordCount.Text))
            {
                lblCurrentIndex.Text = Convert.ToString(Convert.ToInt32(lblCurrentIndex.Text) + Convert.ToInt32(lblPageSize.Text));
            }
            phanTrang();
        }
    }
}